package com.xs.shop.content.service.controller;

import com.xs.shop.common.PageResult;
import com.xs.shop.common.Result;
import com.xs.shop.content.model.entity.DocCategory;
import com.xs.shop.content.service.service.DocCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  文档分类前端控制器
 * </p>
 *
 * @author xs
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/docCategory")
@Tag(name = "文档分类管理", description = "文档分类的CRUD操作")
public class DocCategoryController {

    @Autowired
    private DocCategoryService docCategoryService;

    @PostMapping
    @Operation(summary = "新增文档分类", description = "创建一个新的文档分类")
    public Result<DocCategory> addCategory(@RequestBody DocCategory docCategory) {
        DocCategory savedCategory = docCategoryService.addCategory(docCategory);
        return Result.success(savedCategory);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除文档分类", description = "根据ID软删除文档分类")
    public Result<Boolean> deleteCategory(@PathVariable @Parameter(description = "文档分类ID") Long id) {
        boolean result = docCategoryService.deleteCategory(id);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新文档分类", description = "更新文档分类信息")
    public Result<DocCategory> updateCategory(@RequestBody DocCategory docCategory) {
        DocCategory updatedCategory = docCategoryService.updateCategory(docCategory);
        return Result.success(updatedCategory);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取文档分类详情", description = "根据ID获取文档分类详情")
    public Result<DocCategory> getCategoryById(@PathVariable @Parameter(description = "文档分类ID") Long id) {
        DocCategory category = docCategoryService.getCategoryById(id);
        return Result.success(category);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询文档分类", description = "分页查询文档分类列表，支持按名称模糊查询和显示状态查询")
    public PageResult<DocCategory> pageCategory(
            @RequestParam(defaultValue = "1") @Parameter(description = "当前页码") Integer current,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页记录数") Integer pageSize,
            @RequestParam(required = false) @Parameter(description = "分类名称(模糊查询)") String name,
            @RequestParam(required = false) @Parameter(description = "是否显示(0:不显示 1:显示)") Byte isShow
    ) {
        return PageResult.success(docCategoryService.pageCategory(current,pageSize, name, isShow)) ;
    }
}
